import {
	Local
} from '../Local/Local.js'

import {
	Utils
} from '../Utils/Utils.js'

import {
	Camera
} from '../Camera/Camera.js'

import {
	Coord
} from '../Coord/Coord.js'

import {
	Event
} from '../Event/Event.js'
import {
	TileLayer
} from '../Layer/TileLayer.js'

import {
	ModelLayer
} from '../Layer/ModelLayer.js'

/**
 * 繪圖
 */
import {
	GraphicsLayer
} from "../Layer/GraphicsLayer.js"
//编辑助手
import {
	EditHelper
} from '../Helper/EditHelper.js'

import {
	Util
} from "../Utils/Util.js"

class App extends Cesium.Viewer {
	constructor(container, option) {
		let options = Cesium.defaultValue(option, {});
		let control = Cesium.defaultValue(options.control, {});

		let viewerParam = {
			animation: Cesium.defaultValue(control.animation, true),
			baseLayerPicker: Cesium.defaultValue(control.baseLayerPicker, true),
			fullscreenButton: Cesium.defaultValue(control.fullscreenButton, true),
			vrButton: Cesium.defaultValue(control.vrButton, false),
			geocoder: Cesium.defaultValue(control.geocoder, true),
			homeButton: Cesium.defaultValue(control.homeButton, true),
			infoBox: Cesium.defaultValue(control.infoBox, true),
			sceneModePicker: Cesium.defaultValue(control.sceneModePicker, true),
			selectionIndicator: Cesium.defaultValue(control.selectionIndicator, true),
			timeline: Cesium.defaultValue(control.timeline, true),
			navigationHelpButton: Cesium.defaultValue(control.navigationHelpButton, true),
			navigationInstructionsInitiallyVisible: Cesium.defaultValue(control
				.navigationInstructionsInitiallyVisible, true),
			scene3DOnly: Cesium.defaultValue(control.scene3DOnly, false),
			shouldAnimate: Cesium.defaultValue(control.shouldAnimate, false),
			useDefaultRenderLoop: Cesium.defaultValue(control.useDefaultRenderLoop, true),
			targetFrameRate: Cesium.defaultValue(control.targetFrameRate, undefined),
			showRenderLoopErrors: Cesium.defaultValue(control.showRenderLoopErrors, false),
			requestRenderMode: Cesium.defaultValue(control.requestRenderMode, false),
			maximumRenderTimeChange: Cesium.defaultValue(control.maximumRenderTimeChange, 0.0),
			projectionPicker: Cesium.defaultValue(control.projectionPicker, false),
			shadows: Cesium.defaultValue(control.shadows, false),
			contextOptions: {
				webgl: {
					alpha: true,
					depth: true,
					stencil: true,
					antialias: true,
					premultipliedAlpha: true,
					preserveDrawingBuffer: true,
					failIfMajorPerformanceCaveat: true
				}
			},
		};

		//底图控制
		if (options.imageryViewModels && options.imageryViewModels.length > 0) {
			viewerParam.imageryProviderViewModels = Utils.getImageryProviderViewModels(options
				.imageryViewModels);
			if (control.baseLayerPicker) {
				viewerParam.selectedImageryProviderViewModel = viewerParam.imageryProviderViewModels[0];
			} else {
				viewerParam.imageryProvider = viewerParam.imageryProviderViewModels[0].layer;
			}
		} else {
			viewerParam.imageryProviderViewModels = Utils.getImageryProviderViewModels();
			viewerParam.imageryProvider = viewerParam.imageryProviderViewModels[0].layers;
		}

		//地形控制
		// if(options.terrainViewModels && options.terrainViewModels.length>0){
		// 	viewerParam.terrainProviderViewModels = Utils.getTerrainProviderViewModels(options.terrainViewModels);
		// 	if(control.baseLayerPicker){
		// 		viewerParam.selectedTerrainProviderViewModel = viewerParam.terrainProviderViewModels[0];
		// 	}else{
		// 		viewerParam.terrainProvider= viewerParam.terrainProviderViewModels[0].layer;
		// 	}
		// }else{
		// 	viewerParam.terrainProviderViewModels = Utils.getTerrainProviderViewModels();
		// 	viewerParam.terrainProvider= viewerParam.terrainProviderViewModels[0].layer;
		// }

		//天空盒
		if (Cesium.defined(options.skyBox)) {
			if (Cesium.defined(options.skyBox.positiveX) && Cesium.defined(options.skyBox.negativeX) && Cesium
				.defined(options.skyBox.positiveY) &&
				Cesium.defined(options.skyBox.negativeY) && Cesium.defined(options.skyBox.positiveZ) && Cesium
				.defined(options.skyBox.negativeZ)
			) {
				viewerParam.skyBox = new Cesium.SkyBox({
					sources: {
						positiveX: options.skyBox.positiveX,
						negativeX: options.skyBox.negativeX,
						positiveY: options.skyBox.positiveY,
						negativeY: options.skyBox.negativeY,
						positiveZ: options.skyBox.positiveZ,
						negativeZ: options.skyBox.negativeZ
					}
				});
			} else {
				console.error('天空盒参数有误！');
			}
		}
		super(container, viewerParam);

		if (Cesium.defaultValue(control.navigationHelpButton, true)) {
			this._local = new Local(this)
		}
		this.options = options;
		//场景设置
		if (this.options && this.options.scene) {
			this.scene.sun.show = Cesium.defaultValue(this.options.scene.showSun, false); //太阳
			this.scene.moon.show = Cesium.defaultValue(this.options.scene.showMoon, false); //月亮
			this.scene.skyBox.show = Cesium.defaultValue(this.options.scene.showSkyBox, false); //天空盒
			this.scene.skyAtmosphere.show = Cesium.defaultValue(this.options.scene.showSkyAtmosphere,
				false); //大气层
			this.scene.fog.enabled = Cesium.defaultValue(this.options.scene.fog, false);
			this.scene.fxaa = Cesium.defaultValue(this.options.scene.fxaa, false);
			let viewer = this;
			if (this.options.scene.center) {
				if (Cesium.defaultValue(control.homeButton, true)) {
					this.homeButton.viewModel.command.beforeExecute.addEventListener(function(e) {
						e.cancel = true;
						viewer.camera.flyTo({
							destination: Cesium.Cartesian3.fromDegrees(options.scene.center.x,
								options
								.scene.center.y, options.scene.center.z),
							orientation: { //旋转角度
								heading: Cesium.defaultValue(Cesium.Math.toRadians(options.scene
									.center
									.heading || 0), 0.6068261546578739),
								pitch: Cesium.defaultValue(Cesium.Math.toRadians(options.scene
									.center
									.pitch || 0), -0.6663290837739155),
								roll: 0.0
							}
						});
					});
				}
				viewer.camera.flyTo({
					destination: Cesium.Cartesian3.fromDegrees(options.scene.center.x, options
						.scene.center.y, options.scene.center.z),
					orientation: { //旋转角度
						heading: Cesium.defaultValue(Cesium.Math.toRadians(options.scene.center
							.heading || 0), 0.6068261546578739),
						pitch: Cesium.defaultValue(Cesium.Math.toRadians(options.scene.center
							.pitch || 0), -0.6663290837739155),
						roll: 0.0
					}
				});
			}
			if (this.options.scene.globe) {
				this.scene.globe.showGroundAtmosphere = Cesium.defaultValue(this.options.scene.globe
					.showGroundAtmosphere, false);
				this.scene.globe.depthTestAgainstTerrain = Cesium.defaultValue(this.options.scene.globe
					.depthTestAgainstTerrain, false);
				this.scene.globe.baseColor = Cesium.defaultValue(this.options.scene.globe.baseColor,
					undefined);
				this.scene.globe.show = Cesium.defaultValue(this.options.scene.globe.show, true);
				this.scene.globe.terrainExaggeration = Cesium.defaultValue(this.options.scene.globe
					.terrainExaggeration, 1);
			}
			if (this.options.cameraController) {
				this.scene.screenSpaceCameraController.bounceAnimationTime = Cesium.defaultValue(this.options
					.scene.cameraController.bounceAnimationTime, 3.0);
				this.scene.screenSpaceCameraController.enableCollisionDetection = Cesium.defaultValue(this
					.options.scene.cameraController.enableCollisionDetection, true); //启用或禁用相机对地形的碰撞检测。
				this.scene.screenSpaceCameraController.enableInputs = Cesium.defaultValue(this.options.scene
					.cameraController.enableInputs, true);
				this.scene.screenSpaceCameraController.enableLook = Cesium.defaultValue(this.options.scene
					.cameraController.enableLook, true);
				this.scene.screenSpaceCameraController.enableRotate = Cesium.defaultValue(this.options.scene
					.cameraController.enableRotate, true);
				this.scene.screenSpaceCameraController.enableTilt = Cesium.defaultValue(this.options.scene
					.cameraController.enableTilt, true);
				this.scene.screenSpaceCameraController.enableTranslate = Cesium.defaultValue(this.options
					.scene.cameraController.enableTranslate, true);
				this.scene.screenSpaceCameraController.enableZoom = Cesium.defaultValue(this.options.scene
					.cameraController.enableZoom, true);
				this.scene.screenSpaceCameraController.inertiaSpin = Cesium.defaultValue(this.options.scene
					.cameraController.inertiaSpin, 0.9);
				this.scene.screenSpaceCameraController.inertiaTranslate = Cesium.defaultValue(this.options
					.scene.cameraController.inertiaTranslate, 0.9);
				this.scene.screenSpaceCameraController.inertiaZoom = Cesium.defaultValue(this.options.scene
					.cameraController.inertiaZoom, 0.8);
				this.scene.screenSpaceCameraController.maximumMovementRatio = Cesium.defaultValue(this
					.options.scene.cameraController.maximumMovementRatio, 0.1);
				this.scene.screenSpaceCameraController.maximumZoomDistance = Cesium.defaultValue(this.options
					.scene.cameraController.maximumZoomDistance, Number.POSITIVE_INFINITY);
				this.scene.screenSpaceCameraController.minimumCollisionTerrainHeight = Cesium.defaultValue(
					this.options.scene.cameraController.minimumCollisionTerrainHeight, 10000.0);
				this.scene.screenSpaceCameraController.minimumPickingTerrainHeight = Cesium.defaultValue(this
					.options.scene.cameraController.minimumPickingTerrainHeight, 150000.0);
				this.scene.screenSpaceCameraController.minimumTrackBallHeight = Cesium.defaultValue(this
					.options.scene.cameraController.minimumTrackBallHeight, 7500000.0);
				this.scene.screenSpaceCameraController.minimumZoomDistance = Cesium.defaultValue(this.options
					.scene.cameraController.minimumZoomDistance, 1.0);
			}
		} else {
			//是否开启抗锯齿
			this.scene.fxaa = true;
			this.scene.postProcessStages.fxaa.enabled = true;
		}
		this.cesiumWidget.creditContainer.style.display = "none";
		//解决模糊问题
		if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) { //判断是否支持图像渲染像素化处理
			this.resolutionScale = window.devicePixelRatio;
		}
		this.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType
			.LEFT_DOUBLE_CLICK);
		this.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
		this.initAssembly();
	}
	
	set background(background){
		this._background=background;
	} 
	get background(){
		return this._background
	}
	
	/*********************CEarth扩展属性****************************/
	initAssembly() {
		this._cam = new Camera(this);
		this._coord = new Coord(this);
		this._event = new Event(this);
		this._tileLayer = new TileLayer(this);
		this._modelLayer = new ModelLayer(this);
		this._editHelper = new EditHelper(this);
	}

	/**
	 * @description 相机
	 */
	get cam() {
		return this._cam;
	}

	/**
	 * @description 坐标转换
	 */
	get coord() {
		return this._coord;
	}

	/**
	 * @description 编辑助手
	 */
	get editHelper() {
		return this._editHelper;
	}

	/**
	 * @description 事件
	 */
	get event() {
		return this._event;
	}

	/**
	 * @description 工具类
	 */
	get util() {
		if (!this._util) {
			this._util = new Util(this);;
		}
		return this._util;
	}

	/**
	 * @description 瓦片图层
	 */
	get tileLayer() {
		return this._tileLayer;
	}

	/**
	 * @description 模型图层
	 */
	get modelLayer() {
		return this._modelLayer;
	}

	get graphicsLayer() {
		if (!this._graphicsLayer) {
			this._graphicsLayer = new GraphicsLayer(this);
		}
		return this._graphicsLayer;
	}


	/**
	 * @description 创建物体
	 * @param {Object} param{
		type:类型,
	 }
	 */
	create(options) {
		return Utils.convertObjectClass(options)
	}

	/**
	 * @description 使 3D 窗口获得焦点
	 * @param {Object} param
	 */
	focus(param) {}

	/**
	 * @description 物体查询
	 * @param {Object} param
	 */
	query(param) {

	}

	createTerrainLayer(option) {
		let terrain = Utils.convertTerrainLayer(option);
		if (Cesium.defined(terrain)) {
			this.terrainProvider = terrain;
		}
	}

	addTerrainLayer(option) {
		let terrain = Utils.convertTerrainLayer(option);
		if (Cesium.defined(terrain)) {
			this.terrainProvider = terrain;
		}
	}

	/**
	 * @description 事件绑定
	 * @param {Object} options
	 * @param {Object} callback
	 */
	on(options, callback) {
		this._event.on(options, callback);
	}

	/**
	 * @description 移除事件绑定
	 * @param {Object} options
	 * @param {Object} callback
	 */
	off(options, callback) {
		this._event.off(options, callback);
	}

	/**
	 * @description 销毁
	 */
	destroy() {
		if (this._cam) {
			this._cam.destroy();
		}
		if (this._coord) {
			this._coord.destroy();
		}
		if (this._event) {
			this._event.destroy();
		}
		if (this._tileLayer) {
			this._tileLayer.destroy();
		}
		if (this._modelLayer) {
			this._modelLayer.destroy();
		}
		if (this._editHelper) {
			this._editHelper.destroy();
		}
		if (this._graphicsLayer) {
			this._graphicsLayer.destroy();
		}
		delete this._modelLayer
		delete this._tileLayer
		delete this._coord
		delete this._event
		delete this._graphicsLayer
		delete this._editHelper
		delete this._viewer
		return Cesium.destroyObject(this);
	}
}
export {
	App
};
